<!--月考勤列表，某月根据班组筛选的人的出勤情况-->
<template>
  <div class="list-container">
    <el-form class="search-panel" v-model="sercchData" :inline="true">
      <el-form-item label="输入搜索：">
        <el-input
          v-model="searchForm.name"
          placeholder="劳务公司名/班组名/人员姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="出勤时间：">
        <el-date-picker
          style="width: 220px"
          v-model="searchForm.date"
          type="month"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="考勤类型：">
        <el-select v-model="sercchData.attendanceType" placeholder="请选择">
          <el-option
            v-for="item in attendanceTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item style="width: 100px; text-align: right">
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="table-panel">
      <el-row class="table-operate1">
        <el-button
          type="primary"
          :disabled="info.personIds.length == 0 || info.date == date"
          @click="confirm"
        >
          确认考勤
        </el-button>
        <!-- <el-button
          type="primary"
          icon="el-icon-upload2"
          @click="generateMAttendance"
        >
          生成月考勤
        </el-button> -->
      </el-row>
      <el-row>
        <el-col :span="5" style="border-right: 1px solid #e8e8e8">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </el-col>
        <el-col :span="19">
          <el-table
            ref="filterTable"
            :data="tableData"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="45"
              :selectable="selectEnable"
            >
            </el-table-column>
            <el-table-column
              align="center"
              type="index"
              width="55"
              label="序号"
            >
            </el-table-column>
            <el-table-column align="center" prop="personName" label="姓名">
            </el-table-column>
            <!--<el-table-column align="center" label="出勤类型" prop="type">
               <template slot-scope="scope">
                <div>
                  {{
                    scope.row.attendanceType == 0 ? "按天出勤" : "按小时出勤"
                  }}
                </div>
              </template> 
            </el-table-column>-->
            <el-table-column
              v-if="companyId"
              align="center"
              prop="companyConfirm"
              label="确认状态"
              min-width="100"
              :filters="[
                { text: '已确认', value: '已确认' },
                { text: '未确认', value: '未确认' },
              ]"
              :filter-method="filterTag"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.companyConfirm == 1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column
              v-else
              align="center"
              prop="projectConfirm"
              label="确认状态"
              min-width="100"
              :filters="[
                { text: '已确认', value: '已确认' },
                { text: '未确认', value: '未确认' },
              ]"
              :filter-method="filterTag"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.projectConfirm == 1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column
              v-if="companyId"
              align="center"
              prop="teamLeaderConfirm"
              label="班组长确认状态"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.teamLeaderConfirm == 1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column
              v-else
              align="center"
              prop="companyConfirm"
              label="劳务公司确认状态"
              min-width="180"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.companyConfirm == 1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="出勤天数">
              <template slot-scope="scope">
                <div>{{ scope.row.actualDays }}天</div>
              </template>
            </el-table-column>
            <!-- <el-table-column align="center" prop="operater" label="结算人">
            </el-table-column> -->
            <!-- <el-table-column align="center" label="操作时间" min-width="100">
              <template slot-scope="scope">
                <div>{{ scope.row.operateTime }}</div>
              </template>
            </el-table-column> -->
            <el-table-column
              label="操作"
              align="center"
              min-width="200"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-button
                  @click.native.prevent="checkRow(scope.row)"
                  type="text"
                  icon="el-icon-search"
                  >查看</el-button
                >
                <!--自己已确认就不能调整-->
                <el-button
                  @click.native.prevent="editRow(scope.row)"
                  type="text"
                  icon="el-icon-edit"
                  v-if="companyId"
                  :disabled="scope.row.companyConfirm == 1"
                  >调整</el-button
                >
                <el-button
                  @click.native.prevent="editRow(scope.row)"
                  type="text"
                  icon="el-icon-edit"
                  v-else
                  :disabled="scope.row.projectConfirm == 1"
                  >调整</el-button
                >
                <!--如果是劳务公司进来,是当月或劳务公司已确认或者班组长还未确认就不能确认-->
                <el-button
                  @click.native.prevent="confirm(scope.row)"
                  type="text"
                  icon="el-icon-circle-check"
                  v-if="companyId"
                  :disabled="
                    scope.row.date == date ||
                    scope.row.companyConfirm == 1 ||
                    scope.row.teamLeaderConfirm == 0
                  "
                  >确认</el-button
                >
                <!--如果是项目方进来，是当月或项目方已确认或者劳务公司还未确认就不能确认-->
                <el-button
                  @click.native.prevent="confirm(scope.row)"
                  type="text"
                  icon="el-icon-circle-check"
                  v-else
                  :disabled="
                    scope.row.date == date ||
                    scope.row.projectConfirm == 1 ||
                    scope.row.companyConfirm == 0
                  "
                  >确认</el-button
                >
                <!-- <el-button
                  @click.native.prevent="deleteRow(scope.row)"
                  type="text"
                  icon="el-icon-delete"
                  >删除</el-button
                > -->
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row class="page">
        <div class="page-tag">
          共{{ Math.ceil(totalData / searchForm.pageSize) }}页/{{
            totalData
          }}条数据
        </div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="searchForm.currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="searchForm.pageSize"
          layout="sizes, prev, pager, next, jumper"
          :total="totalData"
        >
        </el-pagination>
      </el-row>
    </div>
    <!-- 调整弹窗 -->
    <el-dialog
      class="small-dialog"
      width="600px"
      title="调整考勤"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="dialogForm" :inline="true" style="text-align: center">
        <el-form-item label="考勤天数：" prop="actualDays">
          <el-input v-model="dialogForm.actualDays" placeholder="考勤天数" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTree } from "@/api/worker";
import {
  getMonthAttendanceList,
  deleteMonthAttendanceList,
  corporationConfirm,
  projectConfirm,
  update,
} from "@/api/attendance.js";
export default {
  data() {
    return {
      dialogFormVisible: false,
      sercchData: {
        date: null,
        name: null,
        attendanceType: -1,
      },
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        date: null,
        name: null,
        // status: null,
        teamId: null,
        companyId: null, //是劳务公司进来就有值
        serviceCorporationId: null,
        projectId: localStorage.getItem("projectId"),
      },
      dialogForm: {},
      attendanceTypeOptions: [
        { value: -1, label: "全部" },
        { value: 0, label: "按天" },
        { value: 1, label: "按小时" },
      ],
      flag: 1, //1--一级劳务公司，0--二级班组
      // currentPage: 1,
      // pageSize: 10,
      totalData: 0,
      treeData: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      tableData: [],
      info: {
        date: null,
        personIds: [],
      },
      companyId: null,
      date: null, //当月
      disabled:false
    };
  },
  created() {
    var dd = new Date();
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;
    if (m < 10) m = "0" + m;
    this.date = y + "-" + m;
    // console.log(this.$route)
    this.companyId = localStorage.getItem("companyId");
    this.searchForm.date = this.$route.query.date;
    this.info.date = this.$route.query.date;
    this.getTreeData();
    this.getTableData();
  },
  methods: {
    filterTag(value, row) {
      if (this.companyId) {
        //劳务公司进来
        if (value == "已确认") {
          return row.companyConfirm == 1;
        } else return row.companyConfirm == 0;
      } else {
        if (value == "已确认") {
          return row.projectConfirm == 1;
        } else return row.projectConfirm == 0;
      }
      // return row.tag === value;
    },
    selectEnable(row, rowIndex) {
      if (this.companyId) {
        //劳务公司进来
        if (
          row.teamLeaderConfirm == 0 ||
          row.date == this.date ||
          row.companyConfirm == 1
        ) {
          return false;
        } else return true;
      } else {
        //项目方进来
        if (
          row.date == this.date ||
          row.projectConfirm == 1 ||
          row.companyConfirm == 0
        ) {
          return false;
        } else return true;
      }
    },
    editRow(row) {
      this.dialogFormVisible = true;
      this.dialogForm = {
        actualDays: row.actualDays,
        date: row.date,
        personId: row.personId,
      };
    },
    getMonthDay(year, month) {
      let days = new Date(year, month, 0);
      return days.getDate();
    },
    confirmDialog() {
      let year = this.dialogForm.date.substring(0, 4);
      let month = this.dialogForm.date.substring(5, 7);
      let days = this.getMonthDay(year, month);
      // console.log(days,this.dialogForm);
      if (this.dialogForm.actualDays > days) {
        // console.log('error')
        this.$message({
          message: "请输入正确的出勤天数！",
          type: "warning",
        });
      } else {
        update(this.dialogForm).then((res) => {
          this.$message({
            type: "success",
            message: res.message,
          });
          this.dialogFormVisible = false;
          this.getTableData();
        });
      }
    },
    confirm(row) {
      if (row) {
        console.log(row);
        this.info.personIds.push(row.personId);
        console.log(this.info);
      }
      this.$confirm("是否确认选中人员的月考勤?", "月考勤确认", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.companyId) {
            corporationConfirm(this.info)
              .then((res) => {
                this.info.personIds = [];
                console.log(res);
                this.$message({
                  type: "success",
                  message: res.message,
                });
                this.getTableData();
              })
              .catch((res) => {
                console.log(res);
                this.info.personIds = [];
                this.$message({
                  type: "error",
                  message: res.message,
                });
              });
          } else {
            projectConfirm(this.info)
              .then((res) => {
                this.$message({
                  type: "success",
                  message: res.message,
                });
                this.info.personIds = [];
                this.getTableData();
              })
              .catch((err) => {
                this.info.personIds = [];
                console.log(err.message);
                this.$message({
                  type: "error",
                  message: err.message,
                });
              });
          }
        })
        .catch(() => {
          // this.info.personIds = [];
          this.$message({
            type: "info",
            message: "已取消确认",
          });
        });
    },
    handleSelectionChange(res) {
      var personIds = [];
      res.forEach((item) => {
        personIds.push(item.personId);
      });
      this.info.personIds = personIds;
      // console.log(personIds);
      console.log(this.info.personIds);
    },
    // 获取劳务公司、班组树形数据
    getTreeData() {
      getTree().then((res) => {
        // console.log(res)
        this.treeData = res.data.data;
      });
    },
    onSubmit() {
      this.getTableData();
    },
    //发送请求获取表格数据
    getTableData() {
      if (this.flag === 0) {
        // 根据班组id查询班组人员————选择班组
        getMonthAttendanceList(this.searchForm).then((res) => {
          console.log(res);
          this.tableData = res.data.data.records;
          this.totalData = res.data.data.total;
          // this.processTableData();
        });
      } else {
        // 根据劳务公司id查询劳务公司人员————选择劳务公司
        getMonthAttendanceList(this.searchForm).then((res) => {
          // console.log(res);
          this.tableData = res.data.data.records;
          this.totalData = res.data.data.total;
          // this.processTableData();
        });
      }
    },
    // 树状图
    handleNodeClick(res) {
      // console.log(res)
      if (res.children == null) {
        // console.log("班组")
        this.flag = 0;
        this.searchForm.teamId = res.id;
        this.searchForm.serviceCorporationId = null;
        this.getTableData();
      } else {
        // console.log("劳务公司")
        this.flag = 1;
        this.searchForm.serviceCorporationId = res.id;
        this.searchForm.teamId = null;
        this.getTableData();
      }
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val;
      this.getTableData();
    },
    checkRow(res) {
      console.log("res", res);
      this.$router.push({
        path: "/attendance/month/detail",
        query: {
          personInfo: {
            personId: res.personId,
            personName: res.personName,
            serviceCorporationName: res.serviceCorporationName,
            teamName: res.teamName,
          },
          date: this.searchForm.date,
        },
      });
    },
    deleteRow(res) {
      deleteMonthAttendanceList({
        date: res.date,
        personId: res.personId,
      }).then((e) => {
        this.getTableData();
        this.$message({
          type: "success",
          message: "删除成功！",
        });
      });
    },
  },
};
</script>
<style scoped>
.table-operate1 {
  margin-bottom: 10px;
  text-align: right;
}
</style>